//
//  TcpDelegate.swift
//  SwiftTCPTest
//
//  Created by andyge on 15/8/1.
//  Copyright © 2015年 gejingguo. All rights reserved.
//

import Foundation

import SwiftTCP

class TcpBinaryDelegate: TcpDelegate {
    
    func onTcpServerError(server: TcpServer, err: Error) {
        print("tcpserver failed, \(err.errorMsg), \(err.errorCode)")
    }
    
    func onTcpClinetAddError(server: TcpServer, poller: Poller, err: Error) {
        print("tcpclient addpoller failed, \(err.errorCode), \(err.errorMsg)")
    }
    
    func onTcpClientRead(client: TcpClient) -> Int {
        // 回显，把收到数据回府回去
        let data = client.recvBuf.readAddr
        let size = client.recvBuf.size
        
        let err = client.send(data, size: size)
        if err != nil {
            print("client senddata failed, \(err?.errorCode), \(err?.errorMsg)")
        }
        
        return size
    }
    
    func onTcpClientError(client: TcpClient, err: Error) {
        print("client error, \(err.errorCode), \(err.errorMsg)")
    }
}